home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1995 November
/
EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso
/
earcd
/
util
/
misc
/
executiv.lha
/
Executive_V1.00
/
sysinfo.lzx
/
examples
/
test
/
test.c
< prev
Wrap
C/C++ Source or Header
|
1990-09-28
|
8KB
|
294 lines
/*
* Test-program for some sysinfo.library features
*
* This file is public domain.
*
* Author: Petri Nordlund <petrin@mits.mdata.fi>
*
* $Id: test.c 1.2 1995/07/27 12:28:02 petrin Exp petrin $
*
*/
#include "defs.h"
#include <proto/sysinfo.h>
#include <libraries/sysinfo.h>
/* VARIABLES */
struct sysinfo *si = NULL;
struct Library *SysinfoBase = NULL;
int
main(int argc, char **argv)
{
if(!(SysinfoBase = OpenLibrary(SYSINFONAME, SYSINFOVERSION)))
{
puts("Can't open sysinfo.library");
exit(RETURN_FAIL);
}
/* Initialize the sysinfo.library, this will make connection to the
* server-process and allocate the sysinfo-structure. */
if(!(si = InitSysinfo()))
{
puts("Couldn't initialize sysinfo");
CloseLibrary(SysinfoBase);
exit(RETURN_FAIL);
}
/* print our pid, ppid and pgrp */
printf("pid: %d\n",GetPid(si));
if(si->GetPpid_implemented)
{
int p = GetPpid(si);
if(p != -1)
printf("ppid: %d\n",p);
else
printf("ppid: unknown\n");
}
if(si->GetPgrp_implemented)
{
int p = GetPgrp(si);
if(p != -1)
printf("pgrp: %d\n",p);
else
printf("pgrp: unknown\n");
}
/* If si->which_implemented is 0, then GetNice() and SetNice() are not available */
/* We'll also make sure that the search methods we need have been implemented */
if(si->which_implemented && (si->which_implemented & (WHICHF_PRIO_TASK | WHICH_PRIO_PROCESS)))
{
int nice;
/* display the nice-value for this task */
nice=GetNice(si,WHICH_PRIO_TASK,0);
if(nice == -1)
{
if(si->errno)
printf("GetNice() failed, errno: %d\n",si->errno);
else
printf("nice: %d\n",nice);
}
else
printf("nice: %d\n",nice);
/* set our nice-value to +5 */
if(SetNice(si,WHICH_PRIO_PROCESS,GetPid(si),5))
printf("SetNice() failed, errno: %d\n",si->errno);
}
/* Ask for notify and output load averages every second for 10 seconds. */
if(si->loadavg_type != LOADAVG_NONE)
{
struct sysinfo_notify *not;
struct Message *msg;
short i;
if(si->notify_msg_implemented && (not=AddNotify(si,TRUE, 10)))
{
printf("load averages (%d.%02d, %d.%02d, %d.%02d minutes):\n",
si->loadavg_time1/60, si->loadavg_time1%60,
si->loadavg_time2/60, si->loadavg_time2%60,
si->loadavg_time3/60, si->loadavg_time3%60);
for(i=0;i<10;i++)
{
/* We'll get a message every second. There may be more than
* one message in the port at once. */
Wait(1L<<not->notify_port->mp_SigBit);
while(msg = GetMsg(not->notify_port))
{
struct loadaverage load; /* This will be filled by GetLoadAverage() */
ReplyMsg(msg);
GetLoadAverage(si, &load); /* Ask sysinfo.library for current load averages */
printf("load average:");
switch(si->loadavg_type)
{
case LOADAVG_FIXEDPNT:
/* Convert fixed point values to floating point values */
if(si->loadavg_time1)
printf(" %.2f",(float) load.loadaverage.lavg_fixed.load1 / (float) si->fscale);
else
printf(" N/A");
if(si->loadavg_time2)
printf(" %.2f",(float) load.loadaverage.lavg_fixed.load2 / (float) si->fscale);
else
printf(" N/A");
if(si->loadavg_time3)
printf(" %.2f\n",(float) load.loadaverage.lavg_fixed.load3 / (float) si->fscale);
else
printf(" N/A\n");
break;
case LOADAVG_FLOAT:
/* Load averages are already in floating point format */
if(si->loadavg_time1)
printf(" %.2f",load.loadaverage.lavg_float.load1);
else
printf(" N/A");
if(si->loadavg_time2)
printf(" %.2f",load.loadaverage.lavg_float.load2);
else
printf(" N/A");
if(si->loadavg_time3)
printf(" %.2f\n",load.loadaverage.lavg_float.load3);
else
printf(" N/A\n");
break;
}
}
}
RemoveNotify(si,not);
}
else
printf("Can't use notification.\n");
}
else
printf("Load averages are not supported.\n");
/* output cpu usage values */
{
struct cpu_usage cu;
GetCpuUsage(si,&cu);
printf("cpu time: ");
if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
printf("%d seconds used, %d seconds idle\n",cu.total_used_cputime, cu.total_elapsed_time - cu.total_used_cputime);
else
printf("N/A\n");
printf("cpu usage: ");
if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
printf("%.2f%%\n",((float) cu.total_used_cputime * 100.0) / (float) cu.total_elapsed_time);
else
printf("N/A\n");
printf("current cpu usage: ");
if(si->cpu_usage_implemented & CPU_USAGEF_LASTSEC_IMPLEMENTED)
printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
else
printf("N/A\n");
printf("recent cpu usage: ");
if(si->cpu_usage_implemented & CPU_USAGEF_RECENT_IMPLEMENTED)
printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
else
printf("N/A\n");
printf("context switches: ");
if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_IMPLEMENTED)
printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
else
printf("N/A\n");
printf("total context switches: ");
if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_IMPLEMENTED)
printf("%d\n", cu.total_csw);
else
printf("N/A\n");
printf("current context switches: ");
if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
else
printf("N/A\n");
printf("current total csws: ");
if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
printf("%d\n", cu.total_csw_lastsec);
else
printf("N/A\n");
}
/* output cpu usage values for this task */
{
struct task_cpu_usage cu;
if(!GetTaskCpuUsage(si,&cu,0))
{
printf("This task:\n");
printf("cpu time: ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
printf("%d.%d seconds\n",cu.total_used_cputime / cu.total_used_time_hz, cu.total_used_cputime % cu.total_used_time_hz);
else
printf("N/A\n");
printf("cpu usage: ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
printf("%.2f%%\n",(((float) cu.total_used_cputime) / ((float) cu.total_used_time_hz) * 100.0) / (float) cu.total_elapsed_time);
else
printf("N/A\n");
printf("current cpu usage: ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_LASTSEC_IMPLEMENTED)
printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
else
printf("N/A\n");
printf("recent cpu usage: ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_RECENT_IMPLEMENTED)
printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
else
printf("N/A\n");
printf("context switches: ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_IMPLEMENTED)
printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
else
printf("N/A\n");
printf("total context switches: ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_IMPLEMENTED)
printf("%d\n", cu.total_csw);
else
printf("N/A\n");
printf("context switches (ps): ");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
else
printf("N/A\n");
printf("total context switches (ps):");
if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
printf("%d\n", cu.total_csw_lastsec);
else
printf("N/A\n");
}
else
printf("Can't get CPU usage for this task.\n");
}
if(si)
FreeSysinfo(si);
if(SysinfoBase)
CloseLibrary(SysinfoBase);
return(RETURN_OK);
}